import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.*;

/**
 * the main config panel for configuring the gui
 * 
 * @author Ronny
 * 
 */
public class KomConfigPane extends JPanel {

	/**
	 * getConn, it is auch possible to set the static position and the terminal
	 * then get the Route to the goal
	 */
	private static final long serialVersionUID = 1L;
	private KomMap kommap;
	private TextField xPos = new TextField();
	private TextField yPos = new TextField();
	private String xpos;
	private String ypos;
	public static boolean setPosFlag = false;
	public Timer timer; // the timer animating the positions of moving sunspots
	public boolean timerstarted = false;// to check if the timer is started
	private int speed; // animation speed
	private boolean flagPositioningStarted = false;
	public boolean ifreferenceToSend = false;
	public boolean ifreferenceSet = false;
	private static int counterfortest = 0;
	private ActionListener actionlistenerstartPosforTimer;
	private ActionListener actionlistenershowRefforTimer;
	private boolean ifshowPositionflag = false;
	private MouseListener sendingreferencepointlisterner;
	private boolean ifadjustPositionflag = false;
	private boolean flagSetRefPoints = false;
	private JButton adjustPos = new JButton("Adjust Positioning");
	private JButton sendPos = new JButton("Sending to Server");
	private JButton getReferencePoints = new JButton("Get Reference Points");
	private JButton setreferencepoints = new JButton("Set Reference Points");
	private JButton sendreferencepoints = new JButton("Sending Reference Point");
	private JButton getBeaconSenders = new JButton("Get Beacon Senders");
	private MouseListener mouseAdjustPoslistener;
	private MouseListener mousesendPoslistener;
	private MouseListener mousesetRefPoints;
	private Point referencePoint;
    private final JButton stopPos = new JButton("Stop Posioning");
	public KomConfigPane(KomMap kommap) {
		this.kommap = kommap;
		this.setPreferredSize(new Dimension(200, 400));
		this.setBackground(Color.gray.darker());
		this.add(this.setConnectionButton());
		this.add(this.stopPositioning());
		this.add(this.setCoordinateToServerButton());
		this.add(this.adjustPosition());
		this.add(this.setXlabel());
		this.add(this.setXPos(xpos));
		this.add(this.setYlabel());
		this.add(this.setYPos(ypos));
		this.add(this.getReferencePoints());
		this.add(this.setReferencePoints());
		this.add(this.sendReferencePointstoServer());
		this.add(this.closeProgramm());
		this.setVisible(true);
	}

	/**
	 * stop postioning
	 * 
	 * @return
	 */
	private JButton stopPositioning() {
		 
		stopPos.setEnabled(false);
		stopPos.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (stopPos.isEnabled()) {
					
					kommap.guiController.stopPositioning();
				}
			}
		});
		return stopPos;
	}

	/**
	 * sendReferencePointstoServer Button
	 * 
	 * @return
	 */
	private JButton sendReferencePointstoServer() {

		sendingreferencepointlisterner = new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				sendreferencepoints.setEnabled(false);
			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			// for our test there can only one reference be tested at one
			// time
			// which is then sent to the server. So at one time only one
			// reference is allowed to be set ont the kommap panel

			@Override
			public void mouseClicked(MouseEvent e) {

				Component[] com = kommap.komMapPane.getComponents();
				for (int a = 0; a < com.length; a++) {
					if (!com[a].getComponentListeners().equals(
							kommap.komMapPane.popupListener)) {
						com[a].addMouseListener(kommap.komMapPane.popupListener);
					}
				}

				ifreferenceSet = false;
				kommap.komconfigpane.sendreferencepoints
						.removeMouseListener(sendingreferencepointlisterner);

				// another reference can be set on the panel
				kommap.komMapPane.refPointtoServerFlag = true;

				if (PopupListener.xpos != -10 && PopupListener.xpos != -10
						&& xpos != null && ypos != null) {
					referencePoint = new Point(new Integer(xpos).intValue(),
							new Integer(ypos).intValue());
					kommap.guiController.addReferencePoint(referencePoint.x,
							referencePoint.y, 10); // samplecount
													// =10

					// referencePointMap.put(referencePoint,kommap.komMapPane.refPoint);
					// referencePoint will not be dragged after the
					// reference
					// point
					// having been sent to the server

					kommap.komMapPane.refPoint
							.removeMouseMotionListener(kommap.komMapPane.mousemotionlistenerforRef);
				}
				if (flagSetRefPoints) {
					kommap.komconfigpane.setreferencepoints
							.setEnabled(flagSetRefPoints);
					setreferencepoints.addMouseListener(mousesetRefPoints);
					flagSetRefPoints = false;
				}
			}

		};

		sendreferencepoints.setEnabled(false);

		return sendreferencepoints;
	}

	/**
	 * set reference points
	 * 
	 * @return
	 */
	private JButton setReferencePoints() {

		mousesetRefPoints = new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			@Override
			public void mouseClicked(MouseEvent e) {

				// if setRefPoints clicked, the button should be deactivated
				// until refreshed
				// send positioning ---> timer gestarted ---> set Reference
				// point
				// ---> timer gestopped

				if (timerstarted) {
					kommap.komconfigpane.timer.stop();
					timerstarted = false;
				}

				// activate the sending reference server button
				ifreferenceSet = true;
				
				
				
				kommap.komconfigpane.sendreferencepoints
						.setEnabled(ifreferenceSet);
				
				kommap.komconfigpane.sendreferencepoints
						.addMouseListener(sendingreferencepointlisterner);

				if (!flagSetRefPoints && setreferencepoints.isEnabled()) {
					JOptionPane.showMessageDialog(null,
							" Please Click on the Map to set reference Points",
							"Reference Points Setting",
							JOptionPane.INFORMATION_MESSAGE);
					kommap.komMapPane.setRefPoints();
					setreferencepoints.removeMouseListener(mousesetRefPoints);
				}

				setreferencepoints.setEnabled(flagSetRefPoints);
				flagSetRefPoints = true;

				// remove all the popuplistener
				Component[] com = kommap.komMapPane.getComponents();
				System.out.println("com.length" + com.length);
				for (int a = 0; a < com.length; a++) {
					com[a].removeMouseListener(kommap.komMapPane.popupListener);
					com[a].removeMouseListener(kommap.komMapPane.popupListener);
				}
			}
		};

		setreferencepoints.addMouseListener(mousesetRefPoints);

		return setreferencepoints;
	}

	/**
	 * set label for x coordinate
	 * 
	 * @return
	 */
	private JLabel setXlabel() {
		JLabel positionlabelx = new JLabel("Coordinator X: ", JLabel.RIGHT);
		return positionlabelx;
	}

	/**
	 * get the x coordinate
	 * 
	 * @param xpos
	 * @return
	 */
	public TextField setXPos(String xpos) {
		this.xpos = xpos;
		xPos.setSize(new Dimension(40, 20));
		xPos.setText(xpos);

		return xPos;
	}

	/**
	 * get the y coordinate
	 * 
	 * @param ypos
	 * @return
	 */
	public TextField setYPos(String ypos) {
		this.ypos = ypos;
		yPos.setSize(new Dimension(40, 20));
		yPos.setText(ypos);

		return yPos;
	}

	/**
	 * set label for y coordinate
	 * 
	 * @return
	 */
	private JLabel setYlabel() {
		JLabel positionlabely = new JLabel("Coordinator Y: ", JLabel.RIGHT);

		return positionlabely;
	}

	/**
	 * adjust Positioning
	 */
	private JButton adjustPosition() {

		adjustPos.setEnabled(ifshowPositionflag);

		mouseAdjustPoslistener = new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			@Override
			public void mouseClicked(MouseEvent e) {

				JOptionPane.showMessageDialog(null,
						" Please Click on the Map to set your Position",
						"Adjust Postioning", JOptionPane.INFORMATION_MESSAGE);

				kommap.komMapPane.givePosiFlag();
				if (timerstarted) {
					kommap.komconfigpane.timer.stop();
					timerstarted = false;
				}
				kommap.komconfigpane.ifadjustPositionflag = true;
				System.out.println("adjustPos.getX()" + adjustPos.getX()
						+ "adjustPos.getY()" + adjustPos.getY());
				kommap.komconfigpane.sendPos.setEnabled(ifadjustPositionflag);
				kommap.komconfigpane.sendPos
						.addMouseListener(mousesendPoslistener);
			}
		};

		return adjustPos;
	}

	/**
	 * getReferencePoints
	 */
	private JButton getReferencePoints() {

		actionlistenershowRefforTimer = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// kommap.komconfigpane.timer.stop();
				kommap.guiController.getXYCoordinates();
				kommap.komMapPane.showReferencePoints(kommap.guiController
						.getReferencepoints());

				// kommap.komconfigpane.timer.start();
			}
		};
		MouseListener mouselistener = new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				kommap.komMapPane.showReferencePoints(kommap.guiController
						.getReferencepoints());
				Component[] com = kommap.komMapPane.getComponents();
				for (int a = 0; a < com.length; a++) {
					if (!com[a].getComponentListeners().equals(
							kommap.komMapPane.popupListener)) {
						com[a].addMouseListener(kommap.komMapPane.popupListener);
					}
				}

			}
		};
		getReferencePoints.addMouseListener(mouselistener);

		return getReferencePoints;
	}

	/**
	 * 
	 * @return
	 */
	private JButton setCoordinateToServerButton() {

		sendPos.setEnabled(ifadjustPositionflag);
		mousesendPoslistener = new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				
				// send to the xy coordinate to server
				
				if (PopupListener.xpos != -10 && PopupListener.ypos != -10
						&& xpos != null && ypos != null) {
					int xpos = new Integer(kommap.komconfigpane.xPos.getText())
							.intValue();
					int ypos = new Integer(kommap.komconfigpane.yPos.getText())
							.intValue();
					String spotID = "0000";
					kommap.guiController.sendDatetoServer(spotID, xpos, ypos);
				}

			}
		};

		return sendPos;
	}

	/**
	 * Start Positioning
	 */
	private JButton setConnectionButton() {
		
		final JButton setConn = new JButton("Start Positioning");
		flagPositioningStarted = true;

		actionlistenerstartPosforTimer = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				kommap.guiController.getXYCoordinates();

			}
		};
		setConn.addActionListener(actionlistenerstartPosforTimer);
		setConn.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {
				stopPos.setEnabled(true);
				kommap.komconfigpane.ifshowPositionflag = true;
				kommap.komconfigpane.adjustPos.setEnabled(ifshowPositionflag);
				kommap.komconfigpane.adjustPos
						.addMouseListener(mouseAdjustPoslistener);
				kommap.guiController.getXYCoordinates();
				timer = new Timer(100,
						kommap.komconfigpane.actionlistenerstartPosforTimer);
				timer.setInitialDelay(200);
				timer.start();
				kommap.komconfigpane.timerstarted = true;

				if (flagSetRefPoints) {
					kommap.komconfigpane.setreferencepoints
							.setEnabled(flagSetRefPoints);
					setreferencepoints.addMouseListener(mousesetRefPoints);
					flagSetRefPoints = false;
				}

				System.out.println("setConn  .getX()" + setConn.getX()
						+ "setConn  .getY()" + setConn.getY());
			}

			@Override
			public void mouseExited(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			@Override
			public void mouseClicked(MouseEvent e) {

			}

		});
		return setConn;

	}

	/**
	 * exit programm button
	 */
	private JButton closeProgramm() {
		JButton closePro = new JButton("Close Programm");
		MouseListener mouselistener = new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				kommap.dispose();
				System.exit(0);

			}
		};
		closePro.addMouseListener(mouselistener);
		return closePro;

	}

}
